package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.shopping.service.ICartOrderService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Set;

/**
 * @author Trellis
 * @since 2022/09/21 11:30
 */
@Service
public class  ICarOrderServiceImpl implements ICartOrderService {

    @Autowired
    RedissonClient redissonClient;

    @Override
    public void clearCartItemByUserID(ClearCartItemRequest request) {
        RMap<Object, Object> map = redissonClient.getMap(request.getUserId().toString());
        @NotEmpty
        List<Long> productIds = request.getProductIds();
        /*Set<Object> objects = map.keySet();
        for (Object object : objects) {
            for (Long productId : productIds) {
                if (object.equals(productId)) {
                    map.remove(object);
                }
            }
        }*/
        Set<Object> objects = map.keySet();
        for (Object object : objects) {
            for (Long productId : productIds) {
                String redisProductId = (String) object;
                if (redisProductId.equals((productId).toString())) {
                    map.remove(object);
                }
            }
        }
    }
}
